package com.sec.android.app.myfiles.feature.Hover;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.facade.MyFilesFacade;
import com.sec.android.app.myfiles.facade.cmd.DeleteRecordCmd;
import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.util.DrmUtils;

import java.util.ArrayList;

public abstract class AbsHoverView {
    protected AbsMyFilesFragment mFragment;
    protected Context mContext;
    protected FileRecord mRecord;
    protected View mHoverView;

    protected float mAirViewMaxWidth;
    protected float mAirViewMaxHeight;

    protected abstract int getLayoutId();

    protected abstract void initView();

    protected abstract void onClose();

    public AbsHoverView(Context context, FileRecord record, AbsMyFilesFragment fragment) {
        mFragment = fragment;
        mContext = context;
        mContext.setTheme(R.style.MyFiles_Default);
        mRecord = record;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mHoverView = inflater.inflate(getLayoutId(), null);

        initView();
        setBtn();
    }

    private void setBtn() {

        mHoverView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyFilesFacade.executeRecord(mFragment.getProcessId(), mFragment.getActivity(), mRecord);
            }
        });

        View airViewButtonLayout = mHoverView.findViewById(R.id.air_button_include_layout);
        if (airViewButtonLayout != null) {
            ImageButton shareButton = (ImageButton) airViewButtonLayout.findViewById(R.id.air_button_share_via);
            if (shareButton != null) {
                shareButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View arg0) {
                        MyFilesFacade.shareVia(mFragment.getProcessId(), mContext, mFragment, makeFileRecordList(mRecord), false);
                    }
                });

                if (!DrmUtils.isForwardable(mContext, mRecord.getFullPath())) {
                    shareButton.setVisibility(View.GONE);
                }
            }

            final ImageButton deleteButton = (ImageButton) airViewButtonLayout.findViewById(R.id.air_button_delete_via);
            if (deleteButton != null) {
                deleteButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View arg0) {
                        if (mRecord.getStorageType() == FileRecord.StorageType.Recent) {
                            MyFilesFacade.deleteRecord(mFragment.getProcessId(), mContext, mFragment,
                                    makeFileRecordList(mRecord), DeleteRecordCmd.OperationProgress.DO_OPERATE);
                        } else {
                            MyFilesFacade.deleteRecord(mFragment.getProcessId(), mContext, mFragment,
                                    makeFileRecordList(mRecord), DeleteRecordCmd.OperationProgress.SHOW_SINGLE_CONFIRM_POPUP);
                        }
                    }
                });
            }
        }
    }

    private ArrayList<FileRecord> makeFileRecordList(FileRecord record) {
        ArrayList<FileRecord> ret = new ArrayList<>();
        ret.add(record);

        return ret;
    }

    public View getView() {
        return mHoverView;
    }
}
